package mason.dp.observer;

import java.util.ArrayList;

/**
 * 被观察者的具体实现类
 * @author MasonAir
 *
 */
public class WeatherData implements Subject {
	//记录观察者
	private ArrayList<Observer> observers;
	private float temperature;//温度
	private float humidity;//湿度
	private float pressure;//气压
	
	public WeatherData() {
		//构造器重建立观察者列表
		observers = new ArrayList<Observer>();
	}
	@Override
	public void registerObserver(Observer o) {
		observers.add(o);
	}

	@Override
	public void removeObserver(Observer o) {
		int index = observers.indexOf(o);
		if(index>=0) {
			observers.remove(index);
		}

	}

	@Override
	public void notifyObservers() {
		for (Observer obs : observers) {
			obs.update(temperature, humidity, pressure);
		}
	}
	
	/**
	 * 当从气象站得到更新数据时，通知观察者
	 */
	public void measurementsChanged() {
		notifyObservers();
	}
	
	/**
	 * 模拟数据的改变
	 * @param temperature
	 * @param humidity
	 * @param pressure
	 */
	public void setMeasurements(float temperature,float humidity,float pressure) {
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
		measurementsChanged();
	}

}
